#!/usr/bin/env python

import os
import shutil
import subprocess
import ConfigParser


class Environment:
    def __init__(self, build_path):
        self.build_path = build_path
        self.__setup__(self.build_path)
        self.__make__()

    def __setup__(self, build_path):
        if os.path.isdir(build_path):
            try:
                qclassify_path = str(build_path) + "/qclassify/"
                qclassify_svn = "/root/qasvn/scripts2/NAVL/qclassify/"
                if os.path.isdir(qclassify_svn):
                    shutil.copy(qclassify_svn + "qconfig.py", qclassify_path)
                    shutil.copy(qclassify_svn + "restart_qclassify.sh", qclassify_path)
                else:
                    print "Path does not exist: %s" % qclassify_svn
            except Exception, error_msg:
                print error_msg
        else:
            print "Path does not exist: %s" % build_path

    def __make__(self):
        capreader_path = self.build_path + "/capreader/"
        qclassify_path = self.build_path + "/qclassify/"
        make_clean_qclassify = "make clean -C " + qclassify_path
        make_clean_capreader = "make clean -C " + capreader_path
        make_qclassify = "make -C " + qclassify_path
        make_capreader = "make -C " + capreader_path
        chmod_qconfig = "chmod +x qconfig.py"
        chmod_re_qclassify = "chmod +x restart_qclassify.sh"
        if os.path.isdir(capreader_path):
            if os.path.isdir(qclassify_path):
                try:
                    subprocess.call(make_clean_capreader, shell=True)
                    subprocess.call(make_clean_qclassify, shell=True)
                    subprocess.call(make_capreader, shell=True)
                    subprocess.call(make_qclassify, shell=True)
                    subprocess.call(chmod_qconfig, cwd=qclassify_path, shell=True)
                    subprocess.call(chmod_re_qclassify, cwd=qclassify_path, shell=True)
                except Exception, error_msg:
                    print error_msg
            else:
                print "Path does not exist: %s" % qclassify_path
        else:
            print "Path does not exist: %s" % capreader_path


class Config(object):
    def __init__(self, config_file):
        self.flowtool = None
        self.config30 = None
        self.config40 = None
        self.setup(config_file)

    def getConfig30(self):
        return self.config30

    def getConfig40(self):
        return self.config40

    def getCapreader30(self):
        return self.capreader30

    def getCapreader40(self):
        return self.capreader40

    def getFlowtool(self):
        return self.flowtool

    def read(self, config_file):
        try:
            config = ConfigParser.ConfigParser()
            config.read(config_file)
            self.flowtool = config.get('environment', 'flowtool')
            self.config30 = config.get('environment', 'config30')
            self.config40 = config.get('environment', 'config40')
        except:
            self.flowtool = '/home/dev/Desktop/flowtool/flowtool'
            self.config30 = '/home/dev/Desktop/flowtool/config30.config'
            self.config40 = '/home/dev/Desktop/flowtool/config40.config'

        print 'Finished reading: ' + config_file


    def create(self, config_file):
        config = ConfigParser.RawConfigParser()
        config.add_section('environment')
        config.set('environment', 'flowtool', '/home/dev/Desktop/flowtool/flowtool')
        config.set('environment', 'config30', '/home/dev/Desktop/flowtool/config30.config')
        config.set('environment', 'config40', '/home/dev/Desktop/flowtool/config40.config')
        with open(config_file, 'wb') as file:
            config.write(file)
        file.close()
        print 'Finished creating: ' + config_file

    def setup(self, config_file):
        if os.path.exists(config_file):
            self.read(config_file)
        else:
            self.create(config_file)
            self.read(config_file)